﻿Public Class Ini
    Public Declare Unicode Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
    Public Declare Unicode Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringW" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32


    Public Function INIRead(ByVal INIpath As String, ByVal sectionName As String, ByVal keyName As String, ByVal defaultValue As String) As String

        Dim n As Int32
        Dim sData As String
        sData = Space(1024)
        n = GetPrivateProfileString(sectionName, keyName, defaultValue, sData, sData.Length, INIpath)
        If n > 0 Then
            INIRead = sData.Substring(0, n)
        Else
            INIRead = ""
        End If

    End Function

#Region "INIRead Overloads"

    Public Function INIRead(ByVal INIpath As String, ByVal sectionName As String, ByVal keyName As String) As String
        Return INIRead(INIpath, sectionName, keyName, "")
    End Function

    Public Function INIRead(ByVal INIpath As String, ByVal sectionName As String) As String
        Return INIRead(INIpath, sectionName, Nothing, "")
    End Function

    Public Function INIRead(ByVal INIpath As String) As String
        Return INIRead(INIpath, Nothing, Nothing, "")
    End Function

#End Region

#Region "INIWrite Overloads"
    Public Function INIWrite(ByVal INIPath As String, ByVal sectionName As String, ByVal keyName As String, ByVal theValue As String) As Int32
        Return WritePrivateProfileString(sectionName, keyName, theValue, INIPath)
    End Function

    Public Function INIDelete(ByVal INIPath As String, ByVal sectionName As String, ByVal keyName As String) As Int32
        Return WritePrivateProfileString(sectionName, keyName, Nothing, INIPath)
    End Function

    Public Function INIDelete(ByVal INIPath As String, ByVal sectionName As String) As Int32
        Return WritePrivateProfileString(sectionName, Nothing, Nothing, INIPath)
    End Function
#End Region
End Class
